package cn.pzx.main_module.ui.fragment

import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import cn.pzx.base_module.base.BaseFragment
import cn.pzx.base_module.widget.ITabView
import cn.pzx.base_module.widget.QTabView
import cn.pzx.base_module.widget.TabView
import cn.pzx.base_module.widget.VerticalTabLayout
import cn.pzx.main_module.bean.ClassifyBean
import cn.pzx.main_module.bean.TabBean
import cn.pzx.main_module.databinding.FragmentClassifyBinding
import cn.pzx.main_module.ui.vm.ClassifyFragmentVM
import java.util.*

/**
 * @author pzx
 * @description:
 * @date :3/16/21 2:58 PM
 */
class ClassifyFragment : BaseFragment<ClassifyFragmentVM, FragmentClassifyBinding>() {


    var tabList: MutableList<TabBean> = mutableListOf()
    override fun initVM() {
    }

    override fun initView() {
//        classifyBeanList = arrayListOf()
        for (i in 0..12) {
            tabList!!.add(TabBean(i, "项目$i"))
            vb.vtabClassify.addTab(
                QTabView(mContext).setTitle(
                    ITabView.TabTitle.Builder().setContent((tabList[i].title).toString()).build()
                )
            )
        }
        vb.vtabClassify.addOnTabSelectedListener(object : VerticalTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabView?, position: Int) {
                vb.viewpagerClassify.setCurrentItem(position)
            }

            override fun onTabReselected(tab: TabView?, position: Int) {}
        })

        vb.viewpagerClassify.adapter = FAdapter(this)
        vb.viewpagerClassify.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                vb.vtabClassify.setTabSelected(position)
            }

            override fun onPageScrollStateChanged(state: Int) {
                super.onPageScrollStateChanged(state)
            }
        })

    }


    override fun initClick() {
    }

    override fun initData() {


    }


    inner class FAdapter @JvmOverloads constructor(fragment: Fragment) :
        FragmentStateAdapter(fragment) {
        override fun getItemCount(): Int {
            return tabList!!.size
        }

        override fun createFragment(position: Int): Fragment {
            return (ItemFragment.getInstance(position,(tabList[position]).title)) as Fragment
        }
    }


}